package org.summer.generator.writer.impl;

import org.summer.generator.handler.FileCreateHandler;
import org.summer.generator.log.Logger;
import org.summer.generator.log.LoggerManager;
import org.summer.generator.vo.Column;
import org.summer.generator.writer.AbstractFileWriter;
import org.summer.generator.writer.WriterToResource;

/**
 * react antd 表格列文件写入处理类
 */
@WriterToResource(suffix = ".d", extension = FileCreateHandler.TYPESCRIPT_FILE_EXTENSION, toTest = true)
public class TypescriptTypeFileWriter extends AbstractFileWriter {
    private static final Logger log = LoggerManager.getLogger(TypescriptTypeFileWriter.class);
    @Override
    public void execute() {
        channel.writerNewline("type %s = {".formatted(table.getTableUpperName()));
        table.getColumns().forEach(this::createFiled);
        channel.writerNewline("}");
        channel.writerEmptyLine();
        channel.writerNewline("export default %s;".formatted(table.getTableUpperName()));

    }

    private void createFiled(Column column){
        channel.writer(" ");
        String r = "    /**\n" +
                "     * " + column.getRemark() + "\n" +
                "     */";
        String fs = "\t%s: %s;"
                .formatted(column.getColumnName(),column.getTsType());
        channel.writerNewline(r);
        channel.writerNewline(fs);
    }
}
